package com.zretc.support.service.serviceimpl;

import com.zretc.support.dao.UserDao;
import com.zretc.support.entity.Log;
import com.zretc.support.entity.User;
import com.zretc.support.service.LogService;
import com.zretc.support.service.UserService;
import com.zretc.support.tool.AjaxResponse;
import com.zretc.support.tool.MD5Util;
import com.zretc.support.tool.PageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.UUID;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao userDao;
    @Autowired
    LogService logService;

    //返回的类
    AjaxResponse ajaxResponse = new AjaxResponse();
    //日志对象
    Log log = new Log();
    //
    PageData<User> pageData = new PageData<>();
    //日志记录
    Logger logger = LoggerFactory.getLogger(UserServiceImpl.class);

    @Override
    public AjaxResponse addUser(User user) {
        //uuid
        UUID uuid = UUID.randomUUID();
        //将uuid转化为字符串
        String uid = uuid+"";
        //设置user的id
        user.setUserId(uid);
        //初始密码123456,将密码进行加密
        String userpwd=MD5Util.getEncodeByMd5("123456");
        //设置显示状态为1
        user.setUserCondition(1);
        //保存
        user.setUserPwd(userpwd);
        //首次创建用户，默认头像
        user.setUserPic("header");
        //调用save方法添加
        userDao.save(user);

        ajaxResponse.setCode(200);
        ajaxResponse.setMsg("添加成功");
        return ajaxResponse;
    }

    @Override
    public AjaxResponse updUser(User user) {
        logger.info("密码"+user.getUserPwd());
        //调用save方法修改
        userDao.save(user);

        ajaxResponse.setCode(200);
        ajaxResponse.setMsg("修改成功");
        return ajaxResponse;
    }

    @Override
    public AjaxResponse doLogin(String userName, String userPwd,int userRole, HttpSession session) {
        logger.info("role:"+userRole);
        //将密码进行加密
        String up=MD5Util.getEncodeByMd5(userPwd);
        logger.info("up:"+up);
        //登录验证
        User user=userDao.findByUserTelAndUserPwdAndUserRole(userName,up,userRole);
        logger.info("user:"+user);
        //日志详情
        log.setLogOp("用户登录");

        log.setLogObject("user");
        //判断
        if(user==null){
            ajaxResponse.setCode(0);
            ajaxResponse.setMsg("登录失败");
            log.setUser(null);
            log.setLogMark("登录失败");
            //添加对象
            ajaxResponse.setObj(null);
        }else{
            ajaxResponse.setCode(200);
            ajaxResponse.setMsg("登录成功");
            log.setUser(user);
            log.setLogMark("登录成功");
            //登录成功，session存值
            session.setAttribute("userId", user.getUserId());
            session.setAttribute("userName", user.getUserName());
            session.setAttribute("userTel", user.getUserTel());
            //添加对象
            ajaxResponse.setObj(user);
        }
        logger.info("session"+session.getAttribute("userId"));
//      //添加日志
        logService.addLog(log);


        return ajaxResponse;
    }

    /**
     * 分页+模糊查询
     * @param userName 用户姓名
     * @param page 当前页码
     * @param limit 每页的数量
     * @return
     */
    @Override
    public PageData<User> findByLike(String userName, int userRole, int page, int limit) {

        Pageable pageable = PageRequest.of(page,limit);

        Page<User> pagedata = userDao.findByLike(userName,userRole,pageable);
        //每页的集合数据
        pageData.setRecords(pagedata.getContent());
        //当前页
        pageData.setCurrent(pagedata.getNumber()+1);
        //总记录数
        pageData.setTotal((int)pagedata.getTotalElements());
        //每页的记录数
        pageData.setSize(pagedata.getSize());
        //
        pageData.setCode(200);
        //
        pageData.setMsg("查询成功");

        return pageData;
    }

    /**
     * 通过id查询用户
     * @param userId
     * @return
     */
    @Override
    public User findByUserId(String userId) {
        return userDao.findByUserId(userId);
    }

    /**
     * 用于输入框输入值和下拉框获取值最后获取信息
     * @param inputsearch 输入框输入的值
     * @param userRole 用户权限
     * @param page 页码
     * @param limit 条数
     * @param select 查询方式下拉框
     * @param selectsex 性别下拉框
     * @return
     */
    @Override
    public PageData<User> findBySelect(String inputsearch, int userRole, int page, int limit,String select,String selectsex) {

        Pageable pageable = PageRequest.of(page,limit);
        //初始值
        Page<User> pagedata = null;
        //性别不限
        if (selectsex.equals("性别不限")){

            if (select.equals("学生姓名")||inputsearch.equals("")){
                //选择了用户名查询
                pagedata = userDao.findByLike(inputsearch,userRole,pageable);
            }else if(select.equals("联系方式")){
                //选择了联系方式查询
                pagedata = userDao.findByTel(inputsearch,userRole,pageable);
            }
        }else {
            //否则有性别限制
            if (select.equals("学生姓名")||inputsearch.equals("")){
                //选择了用户名查询
                pagedata = userDao.findBySelectU(inputsearch,userRole,selectsex,pageable);
            }else if(select.equals("联系方式")){
                //选择了联系方式查询
                pagedata = userDao.findBySelectT(inputsearch,userRole,selectsex,pageable);
            }
        }
        //每页的集合数据
        pageData.setRecords(pagedata.getContent());
        //当前页
        pageData.setCurrent(pagedata.getNumber()+1);
        //总记录数
        pageData.setTotal((int)pagedata.getTotalElements());
        //每页的记录数
        pageData.setSize(pagedata.getSize());
        //
        pageData.setCode(200);
        //
        pageData.setMsg("查询成功");

        return pageData;
    }

    /**
     * 用于输入框输入值和下拉框获取值最后获取信息
     * @param inputsearch 输入框输入的值
     * @param userRole 用户权限
     * @param page 页码
     * @param limit 条数
     * @param select 查询方式下拉框
     * @param selectsex 性别下拉框
     * @return
     */
    @Override
    public PageData<User> findByTeaSelect(String inputsearch, int userRole, int page, int limit,String select,String selectsex) {

        Pageable pageable = PageRequest.of(page,limit);
        //初始值
        Page<User> pagedata = null;
        //性别不限
        if (selectsex.equals("性别不限")){

            if (select.equals("教师姓名")||inputsearch.equals("")){
                //选择了用户名查询
                pagedata = userDao.findByLike(inputsearch,userRole,pageable);
            }else if(select.equals("联系方式")){
                //选择了联系方式查询
                pagedata = userDao.findByTel(inputsearch,userRole,pageable);
            }
        }else {
            //否则有性别限制
            if (select.equals("教师姓名")||inputsearch.equals("")){
                //选择了用户名查询
                pagedata = userDao.findBySelectU(inputsearch,userRole,selectsex,pageable);
            }else if(select.equals("联系方式")){
                //选择了联系方式查询
                pagedata = userDao.findBySelectT(inputsearch,userRole,selectsex,pageable);
            }
        }
        //每页的集合数据
        pageData.setRecords(pagedata.getContent());
        //当前页
        pageData.setCurrent(pagedata.getNumber()+1);
        //总记录数
        pageData.setTotal((int)pagedata.getTotalElements());
        //每页的记录数
        pageData.setSize(pagedata.getSize());
        //
        pageData.setCode(200);
        //
        pageData.setMsg("查询成功");

        return pageData;
    }
    @Override
    public AjaxResponse updPwd(User user) {
        logger.info("密码"+user.getUserPwd());
        //密码加密
        user.setUserPwd(MD5Util.getEncodeByMd5(user.getUserPwd()));
        //调用save方法修改
        userDao.save(user);

        ajaxResponse.setCode(200);
        ajaxResponse.setMsg("修改成功");
        return ajaxResponse;
    }

    @Override
    public List<User> getusers(int userRole) {
        return userDao.findAllByUserRole(userRole);
    }

    @Override
    public String getusername(String userId) {
        return userDao.getOne(userId).getUserName();
    }

    @Override
    public User findByUserTel(String userTel) {
        return userDao.findByUserTel(userTel);
    }
}
